<template>
  <div class="memberCenterstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>

    <div style="margin: 10px 0" />

    <el-form label-width="80px" :model="datas" size="small">
      <vuedraggable v-model="datas.iconList" v-bind="dragOptions">
        <transition-group>
          <section v-for="(item, index) in datas.iconList" :key="item + index" class="imgBanner">
            <i class="el-icon-circle-close" @click="deleteimg(index)" />
            <!-- 图片 -->
            <div>
              <div v-for="replaceIconIndex in 2" :key="replaceIconIndex" class="imagBox" @click="replaceIcon(replaceIconIndex, index)">
                <img class="imag" :src="replaceIconIndex == 1 ? item.iconPic : item.inactive" draggable="false" />
                <div> {{ replaceIconIndex == 1 ? "选中时" : "未选中时" }} </div>
              </div>
            </div>
            <!-- 标题和链接 -->
            <div class="imgText">
              <el-input v-model="item.name" placeholder="请输入标题，也可不填" size="mini" clearable />
              <!-- 选择类型 -->
              <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink(index)" :linktypeurl="item.url" :theIndex="index" />
            </div>
          </section>
        </transition-group>
      </vuedraggable>

      <el-button class="uploadImg" type="primary" plain @click="showUpload()">
        <i class="el-icon-plus" />添加左侧导航
      </el-button>
    </el-form>

    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>
<script>
import uploadimg from "../../../uploadImg"; // 图片上传
import vuedraggable from "vuedraggable"; // 拖拽组件
import SelectLink from "../../../selectLink";
export default {
  name: "MemberCenterstyle",
  components: { 
    uploadimg, 
    vuedraggable, 
    SelectLink 
  },
  props: {
    datas: Object,
  },
  data() {
    return {
      dragOptions: {
        // 拖拽配置
        animation: 200,
      },
      replaceIconIndex: null,
      replaceIndex: null,
    };
  },
  methods: {
    showUpload(index) {
      this.replaceIndex = index;
      this.$refs.upload.showUpload();
    },
    // 提交
    uploadInformation(res) {
      if (this.replaceIconIndex == 1 && this.replaceIndex != null) {
        this.datas.iconList[this.replaceIndex].iconPic = res;
        this.replaceIconIndex = null;
        return;
      }
      if (this.replaceIconIndex == 2 && this.replaceIndex != null) {
        this.datas.iconList[this.replaceIndex].inactive = res;
        this.replaceIconIndex = null;
        return;
      }
      this.datas.iconList.push({
        name: "",
        id: "",
        iconPic: res,
        inactive: res,
        url: "",
        linktype: ""
      });
    },
    // 选择链接
    selectThisLink(resu) {
      this.datas.iconList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
          it.id = resu.id;
        }
      });
    },
    /* 删除图片列表的图片 */
    deleteimg(index) {
      this.datas.iconList.splice(index, 1);
    },
    /* 点击图片 */
    replaceIcon(replaceIconIndex, replaceIndex) {
      this.replaceIconIndex = replaceIconIndex;
      this.replaceIndex = replaceIndex;
      this.$refs.upload.showUpload();
    },
    clearThisLink (index) {
      this.datas.iconList[index].url = '';
    },
  },
};
</script>
<style lang="scss" scoped>
.memberCenterstyle{
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px 20px;
  box-sizing: border-box;
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }
  .lef {
    .el-form-item__label {
      text-align: left;
    }
  }
  /* 卡片列表 */
  .imgBanner {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片 */
    .imagBox {
      position: relative;
      border-radius: 5px;
      overflow: hidden;
      cursor: pointer;
      .imag {
        width: 60px;
        height: 60px;
      }
      div {
        position: absolute;
        top: 0;
        width: 60px;
        line-height: 60px;
        border-radius: 5px;
        text-align: center;
        font-size: 12px;
        color: #fff;
        background-color: rgba(0, 0, 0, 0.3);
      }
    }

    /* 图片字 */
    .imgText {
      padding-left: 20px;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      justify-content: space-around;
    }
  }
}
</style>
